package com.epoint.mobileoa.tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;

public class GetBitmap {
	// 获得图片的bitmap
	public static Bitmap getBitmap(Context context, int id) {
		return BitmapFactory.decodeResource(context.getResources(), id);
	}

	// 旋转，缩放，图片
	public static Bitmap getZoomRotateBitmap(Bitmap bitmap, int newWidth,
			int newHeight, int rotate) {
		// 获取图片的原始的大小
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		System.out.println("原始的宽高：" + width + "*" + height);
		// 定义缩放的高和宽的比例
		float sw = ((float) newWidth) / width;
		float sh = ((float) newHeight) / height;
		// 创建操作图片的用的Matrix对象
		android.graphics.Matrix matrix = new android.graphics.Matrix();
		// 缩放翻转图片的动作
		// sw sh的绝对值为绽放宽高的比例，sw为负数表示X方向翻转，sh为负数表示Y方向翻转
		matrix.postScale(sw, sh);
		// 旋转30*
		matrix.postRotate(rotate);
		// 创建一个新的图片
		android.graphics.Bitmap resizeBitmap = android.graphics.Bitmap
				.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		System.out.println("缩放后的宽高：" + resizeBitmap.getWidth() + "*"
				+ resizeBitmap.getHeight());
		return resizeBitmap;
	}

	// 往照片上写入日期
	public static Bitmap dateBitmap(Bitmap bitmap, String date, int toWidth,
			int toHeight) {
		if (bitmap == null) {
			return null;
		}

		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		// 创建一个空白的bitmap
		Bitmap newbitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
		Canvas canvas = new Canvas(newbitmap);
//		
		canvas.drawBitmap(bitmap, 0, 0, null);// 在 0，0坐标开始画入src

		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.RED);

		paint.setTextSize(25);
		// canvas.drawText(date, 80, 50, paint);//居顶
//		canvas.drawText(date, toHeight, toWidth, paint);// 居底
		canvas.save(Canvas.ALL_SAVE_FLAG);// 保存
		canvas.restore();// 存储
		return newbitmap;
	}
}
